﻿<?php
require_once ('Controller/basicmodule.inc.php');
require_once ('Controller/Admin/login.inc.php');
require_once ('Controller/Admin/user.inc.php');
require_once ('Controller/Admin/template.inc.php');
class common extends basicmodule {
	protected $template;
	protected $language;
	protected $login;
	protected $user;
	protected $Loader;
	protected $defaultpage = '/?module=CircleManager';
	protected $administratorlinks = array (
//		['title' => 'GUSETBOOK PRESET MESSAGE', 'url' => '?module=GuestBookPresetMessage', 'module' => 'GuestBookPresetMessage'], 
		//['title' => 'DEVICE MANAGER', 'url' => '?module=DeviceManager', 'module' => 'DeviceManager'], 
		['title' => 'CIRCLE MANAGER', 'url' => '?module=CircleManager', 'module' => 'CircleManager'], 
		//['title' => 'MESSAGE MANAGER', 'url' => '?module=MessageManager', 'module' => 'MessageManager'], 
/*		['title' => 'PROMOTION MANAGER', 'url' => '?module=PromotionManager', 'module' => 'PromotionManager'],
		['title' => 'HOUSEKEEPING PRESET MESSAGE', 'url' => '?module=HouseKeepingPresetMessage', 'module' => 'HouseKeepingPresetMessage'],
		['title' => 'MESSAGE SEND MANAGER', 'url' => '?module=MessageSendManager', 'module' => 'MessageSendManager'], */
		['title' => 'LOGOUT', 'url' => '?action=logout', 'module' => '']
	);


	public function __construct () {
		parent::__construct ();
		$this->inituser ();
		$this->initlogin ();
		$this->inittemplate ();
	}

	public function inituser () {
		$this->login = new login ($this->mysql);
	}

	public function initlogin () {
		$this->user = new user ($this->mysql);
	}

	public function inittemplate () {
		$this->template = new template ();
		$this->template->load ('html/index.html');
		$this->template->initTitle ();
		$this->template->initMenu ();
		$this->template->initContent ();
		$this->template->initWarningMesage ();
		$this->template->setTitle ('Management Centre');
	}

	public function showloginpage () {
		$this->login->logout ();
		$logintemplate = new template ();
		$logintemplate->load ('html/login.html');
		$loginform = $logintemplate->dom->documentElement;
		$loginform->setAttribute ('action', '?action=login');
		$loginform = $this->template->dom->importNode ($loginform, true);
		$this->template->content->appendChild ($loginform);
	}

	public function showmessagepage ($url, $message) {
	}

	public function checklogin () {
		if (array_key_exists ('action', $_REQUEST) && $_REQUEST['action'] == 'login') {
			$userresult = $this->user->checklogin ($_REQUEST['loginname'], $_REQUEST['password']);
			if ($userresult['successful'] && $userresult['UserLevel_id'] == 4) {
				$this->login->setLogined ($userresult['User_id']);
				$this->showadministratormenu ();
				header (sprintf ('Location: %s', $this->defaultpage));
			} else {
//error message
				$this->template->addWarningMesage ('Login fail');
				$this->showloginpage ();
			}
		} else if (array_key_exists ('action', $_REQUEST) && $_REQUEST['action'] == 'logout') {
			$this->showloginpage ();
		} else {
			if ($this->login->checkLogined ($this->login->tokenid) && $this->login->userlevel == 4) {
				$this->showadministratormenu ();
				return true;
			} else {
				$this->showloginpage ();
				return false;
			}
		}
	}

	public function execute () {
		$this->checklogin ();
	}

	public function showadministratormenu () {
		$this->showmenu ($this->administratorlinks);
	}

	public function showagentmenu () {
		$this->showmenu ($this->agentlinks);
	}

	public function showmenu ($links) {
		$len = count ($links);
		for ($i=0;$i<$len;$i++) {
			$value = $links[$i];
//header
			$headermenuitem = $this->template->headermenuitem->cloneNode (true);
			$headermenuitemtaga = $headermenuitem->getElementsByTagName ('a')->item (0);
			$headermenuitemtaga->setAttribute ('href', $value['url']);
			$headermenuitemtaga->setAttribute ('title', $value['title']);

			$headermenuitemtagspan = $headermenuitem->getElementsByTagName ('span')->item (0);
			$headermenuitemtagspan->nodeValue = $value['title'];

			if ($this->Loader->module == $value['module']) {
				$headermenuitem->setAttribute ('id', 'current');
			}

			$footermenuitem = $this->template->footermenuitem->cloneNode (true);
			$footermenuitemtaga = $footermenuitem->getElementsByTagName ('a')->item (0);
			$footermenuitemtaga->setAttribute ('href', $value['url']);
			$footermenuitemtaga->setAttribute ('title', $value['title']);
			$footermenuitemtaga->nodeValue = $value['title'];

			if ($i == $len - 1) {
				$headermenuitem->setAttribute ('class', 'last');
				$footermenuitem->setAttribute ('class', 'last');
			}
			$this->template->headermenu->appendChild ($headermenuitem);
			$this->template->footermenu->appendChild ($footermenuitem);


		}

	}

	public function output () {
		echo $this->template->toHTML ();
	}


	public function __destruct () {
		$this->output ();
		parent::__destruct ();
	}

}
?>
